package com.taiyi.renaissancemall.ui.classification.goodlist;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;

import com.taiyi.renaissancemall.Utils.FlowLayoutManager;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.adapter.AdapterManager;
import com.taiyi.renaissancemall.adapter.baseadapter.CommonAdapter;
import com.taiyi.renaissancemall.adapter.baseadapter.MultiItemTypeAdapter;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.FindBrandListBean;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;

//分类商品列表
public class GoodListActivity extends BaseActivity<GoodListView, GoodListPresent> implements GoodListView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.topB)
    TextView topB;
    @BindView(R.id.comprehensiveTv)
    TextView comprehensiveTv;
    @BindView(R.id.salesTv)
    TextView salesTv;
    @BindView(R.id.priceTv)
    TextView priceTv;
    @BindView(R.id.bvPriceTv)
    TextView bvPriceTv;
    @BindView(R.id.filterTv)
    TextView filterTv;
    @BindView(R.id.filter)
    ConstraintLayout filter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.salesIv)
    ImageView salesIv;
    @BindView(R.id.priceIv)
    ImageView priceIv;
    @BindView(R.id.bvIv)
    ImageView bvIv;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.tv11)
    TextView tv11;
    @BindView(R.id.tv22)
    TextView tv22;
    @BindView(R.id.tv33)
    TextView tv33;
    @BindView(R.id.tv44)
    TextView tv44;
    @BindView(R.id.tv55)
    TextView tv55;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    @BindView(R.id.lowPriceEt)
    EditText lowPriceEt;
    @BindView(R.id.maxPriceEt)
    EditText maxPriceEt;
    @BindView(R.id.brandRecyclerView)
    TagFlowLayout mBrandRecyclerView;
    @BindView(R.id.vv)
    View vv;
    @BindView(R.id.determineTv)
    TextView determineTv;
    @BindView(R.id.resetTv)
    TextView resetTv;
    @BindView(R.id.filterLayout)
    ConstraintLayout filterLayout;
    private String class_action_id = "";
    private int page = 1;
    private String class_action_name;
    private boolean isSelectPrice;
    private boolean isSelectBv;
    private boolean isSelectSale;

    private String sale = "";
    private String price = "";
    private String bv = "";
    private GoodListAdapter goodListAdapter;
    private GoodList2Adapter goodList2Adapter;
    private List<GoodListBean.ComlistBean> data = new ArrayList<>();

    private boolean isFirst = false;
    private boolean isFilter = true;
    private Dialog filterDialog;
    private RecyclerView brandRecyclerView;

    private String fbName = "";
    private int fbId = -1;
    private int fbChoose = -1;
    private String fbLow = "";
    private String fbMax = "";
    private List<String> str_ids;
    private List<FindBrandListBean.BrandlistBean> fbrandList = new ArrayList<>();
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView tv6;
    private FindBrandListAdapter findBrandListAdapter;
    StringBuffer sb = new StringBuffer();
    String selectBrandIds = "";
    String categoryid = "";

    private String labelId = "";
    private List<GoodListBean.ComlistBean> lData = new ArrayList<>();
    private List<GoodListBean.ComlistBean> gData = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_good_list;
    }

    @Override
    public GoodListPresent initPresenter() {
        return new GoodListPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        goodListAdapter = new GoodListAdapter(R.layout.search_result_item_layout, data);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(goodListAdapter);

        titleTv.setVisibility(View.VISIBLE);
        comprehensiveTv.setSelected(true);
        titleTv.setTextColor(Color.WHITE);
        rightTitleTv.setVisibility(View.VISIBLE);
        rightTitleTv.setBackgroundResource(R.mipmap.new_two_bottom_icon_no_iv);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
    }


    @Override
    protected void initData() {
        class_action_id = getIntent().getStringExtra("next_class_2_id");
        class_action_name = getIntent().getStringExtra("next_class_2_name");
        labelId = getIntent().getStringExtra("labelId");
        categoryid = getIntent().getStringExtra("categoryid");
        titleTv.setText(class_action_name);
        showProgress();
        //分类商品列表和获取筛选品牌
        presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, selectBrandIds, "", "");
        presenter.getFindBrandList();

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 1.0f).init();
    }


    @Override
    public void initEvent() {
        determineTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgress();
                sb = new StringBuffer();
                vv.setVisibility(View.GONE);
                filterLayout.setVisibility(View.GONE);

                Set<Integer> selectedList = mBrandRecyclerView.getSelectedList();
                Iterator<Integer> iterator = selectedList.iterator();
                while (iterator.hasNext()) {
                    sb.append("," + fbrandList.get(iterator.next()).brandid);
                }
//                for (int i = 0; i < fbrandList.size(); i++) {
//                    if (fbrandList.get(i).isChoose) {
//                        sb.append("," + fbrandList.get(i).brandid);
//                    }
//                }
                if (sb.toString().length() != 0)
                    selectBrandIds = sb.substring(1);
                else
                    selectBrandIds = "";
                presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, selectBrandIds, "", "");

            }
        });
    }

    @Override
    public void getGoodListSuccess(GoodListBean goodListBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        List<GoodListBean.ComlistBean> comlist = goodListBean.comlist;

        if (page == 1) {
            data.clear();
//            lData.clear();
//            gData.clear();
            if (comlist == null || comlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(comlist);
//        lData.addAll(comlist);
//        gData.addAll(comlist);
        if (!isFirst) {//列表
            goodListAdapter.notifyDataSetChanged();
        } else {//双列
            goodList2Adapter.notifyDataSetChanged();
        }

    }


    @Override
    public void getGoodListFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void getFindBrandListSuccess(FindBrandListBean findBrandListBean) {
        fbrandList = findBrandListBean.brandlist;
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
//        FlowLayoutManager flowLayoutManager = new FlowLayoutManager(this, true);
//        mBrandRecyclerView.setLayoutManager(flowLayoutManager);
//        List<FindBrandListBean.BrandlistBean> brandlist = findBrandListBean.brandlist;
//        CommonAdapter goodKindListAdapter = AdapterManager.getGoodKindListAdapter(this, brandlist);
//        mBrandRecyclerView.setAdapter(goodKindListAdapter);
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        mBrandRecyclerView.setAdapter(new TagAdapter<FindBrandListBean.BrandlistBean>(fbrandList) {
            @Override
            public View getView(FlowLayout parent, int position, FindBrandListBean.BrandlistBean o) {
                TextView tv = layoutInflater.inflate(
                        R.layout.new_flow_tv_item_layout,
                        mBrandRecyclerView,
                        false
                ).findViewById(R.id.tv);
                tv.setText(o.brandname);
                return tv;
            }
        });

    }


    @Override
    public void getFindBrandFailure(String msg) {
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void onRefresh() {
        page = 1;
        showProgress();
        presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, selectBrandIds, "", "");
    }

    @Override
    public void onLoadmore() {
        page++;
        showProgress();
        presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, selectBrandIds, "", "");
    }

    @OnClick({R.id.backIv, R.id.rightTitleTv, R.id.comprehensiveTv, R.id.salesTv, R.id.priceTv, R.id.bvPriceTv, R.id.filterTv
            , R.id.tv11, R.id.tv22, R.id.tv33, R.id.tv44, R.id.tv55, R.id.resetTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.rightTitleTv:
                isFirst = !isFirst;
                //列表显示方式
                if (isFirst) {
                    goodList2Adapter = new GoodList2Adapter(R.layout.two_column_item_layout, data);
                    recyclerView.setLayoutManager(new GridLayoutManager(GoodListActivity.this, 2));
                    recyclerView.setAdapter(goodList2Adapter);
                    rightTitleTv.setBackgroundResource(R.mipmap.two_bottom_icon_no_iv);
                } else {
                    goodListAdapter = new GoodListAdapter(R.layout.search_result_item_layout, data);
                    recyclerView.setLayoutManager(new LinearLayoutManager(this));
                    recyclerView.setAdapter(goodListAdapter);
                    rightTitleTv.setBackgroundResource(R.mipmap.new_two_bottom_icon_no_iv);
                }
                break;
            case R.id.comprehensiveTv:
                sale = "";
                price = "";
                bv = "";
                isSelectSale = false;
                isSelectPrice = false;
                isSelectBv = false;
                salesIv.setImageResource(R.mipmap.price_dsc_iv);
                priceIv.setImageResource(R.mipmap.price_dsc_iv);
                bvIv.setImageResource(R.mipmap.price_dsc_iv);
                salesTv.setSelected(false);
                priceTv.setSelected(false);
                bvPriceTv.setSelected(false);
                filterTv.setSelected(false);
                comprehensiveTv.setSelected(true);
                page = 1;
                showProgress();
                presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, "", "", "");
                break;
            case R.id.salesTv:
                price = "";
                bv = "";
                isSelectSale = !isSelectSale;
                isSelectPrice = false;
                isSelectBv = false;
                priceIv.setImageResource(R.mipmap.price_dsc_iv);
                bvIv.setImageResource(R.mipmap.price_dsc_iv);

                if (!isSelectSale) {
                    sale = "1";
                    salesIv.setImageResource(R.mipmap.price_asc_iv);
                } else {
                    sale = "2";
                    salesIv.setImageResource(R.mipmap.price_dsc_iv);
                }
                comprehensiveTv.setSelected(false);
                priceTv.setSelected(false);
                bvPriceTv.setSelected(false);
                filterTv.setSelected(false);
                salesTv.setSelected(true);
                page = 1;
                showProgress();
                presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, "", "", "");
                break;
            case R.id.priceTv:
                sale = "";
                bv = "";
                isSelectPrice = !isSelectPrice;
                isSelectSale = false;
                isSelectBv = false;
                salesIv.setImageResource(R.mipmap.price_dsc_iv);
                bvIv.setImageResource(R.mipmap.price_dsc_iv);
                if (!isSelectPrice) {
                    price = "1";
                    priceIv.setImageResource(R.mipmap.price_asc_iv);
                } else {
                    price = "2";
                    priceIv.setImageResource(R.mipmap.price_dsc_iv);
                }
                comprehensiveTv.setSelected(false);
                salesTv.setSelected(false);
                bvPriceTv.setSelected(false);
                filterTv.setSelected(false);
                priceTv.setSelected(true);
                page = 1;
                showProgress();
                presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, "", "", "");
                break;
            case R.id.bvPriceTv:
                sale = "";
                price = "";
                isSelectBv = !isSelectBv;
                isSelectSale = false;
                isSelectPrice = false;
                salesIv.setImageResource(R.mipmap.price_dsc_iv);
                priceIv.setImageResource(R.mipmap.price_dsc_iv);
                if (!isSelectBv) {
                    bv = "1";
                    bvIv.setImageResource(R.mipmap.price_asc_iv);
                } else {
                    bv = "2";
                    bvIv.setImageResource(R.mipmap.price_dsc_iv);
                }
                comprehensiveTv.setSelected(false);
                salesTv.setSelected(false);
                priceTv.setSelected(false);
                filterTv.setSelected(false);
                bvPriceTv.setSelected(true);
                page = 1;
                showProgress();
                presenter.getGoodList(page + "", labelId, categoryid, class_action_id + "", price, sale, bv, "", "", "");
                break;
            case R.id.filterTv:
                vv.setVisibility(View.VISIBLE);
                filterLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.tv11:
                lowPriceEt.setText("0");
                maxPriceEt.setText("199");
                updateTv();
                tv11.setSelected(true);
                break;
            case R.id.tv22:
                lowPriceEt.setText("200");
                maxPriceEt.setText("499");
                updateTv();
                tv22.setSelected(true);
                break;
            case R.id.tv33:
                lowPriceEt.setText("500");
                maxPriceEt.setText("799");
                updateTv();
                tv33.setSelected(true);
                break;
            case R.id.tv44:
                lowPriceEt.setText("800");
                maxPriceEt.setText("999");
                updateTv();
                tv44.setSelected(true);
                break;
            case R.id.tv55:
                lowPriceEt.setText("1000");
                maxPriceEt.setText("");
                updateTv();
                tv55.setSelected(true);
                break;
            case R.id.resetTv:
                mBrandRecyclerView.getSelectedList().clear();
                mBrandRecyclerView.getAdapter().notifyDataChanged();
                selectBrandIds = "";
                break;
        }
    }

    private void updateTv() {
        tv11.setSelected(false);
        tv22.setSelected(false);
        tv33.setSelected(false);
        tv44.setSelected(false);
        tv55.setSelected(false);
    }


}
